class SWelderWFire extends WelderWFire;

var	SRKFHumanPawn	CachedHealee;

function DoFireEffect()
{
	//Log(Instigator.OwnerName@"used SWelder to weld");
	Super.DoFireEffect();
}

simulated function bool AllowFire()
{
	local KFDoorMover WeldTarget;
	WeldTarget = GetDoor();
	//Log(self@Weapon.FireModeClass[0]@self.IsA('SWelderWFire'));
	//Log(Instigator.OwnerName$" is welding with S Welder");
	// Can't use welder, if no door.
	if ( WeldTarget == none && !CanFindHealee())
	{
		if ( KFPlayerController(Instigator.Controller) != none )
		{
			KFPlayerController(Instigator.Controller).CheckForHint(54);

			if ( FailTime + 0.5 < Level.TimeSeconds )
			{
				PlayerController(Instigator.Controller).ClientMessage(NoWeldTargetMessage, 'CriticalEvent');
				FailTime = Level.TimeSeconds;
			}

		}
		return false;
	}
	if(WeldTarget != none && WeldTarget.bDisallowWeld)
	{
		if( PlayerController(Instigator.controller)!=None )
			PlayerController(Instigator.controller).ClientMessage(CantWeldTargetMessage, 'CriticalEvent');

		return false;
	}
	return Weapon.AmmoAmount(ThisModeNum) >= AmmoPerFire ;
}
// Can we find someone to heal
function bool CanFindHealee()
{
	local SRKFHumanPawn Healtarget;

	Healtarget = GetHealee();
	CachedHealee = Healtarget;

	// Can't use syringe if we can't find a target
	if ( Healtarget == none )
	{
		/*if ( KFPlayerController(Instigator.Controller) != none )
		{
			KFPlayerController(Instigator.Controller).CheckForHint(53);
		} */

		return false;
	}

	// Can't use syringe if our target is already being healed to full health.
	if ( Healtarget.ShieldStrength == 100 )
	{
		return false;
	}

	return true;
}
function SRKFHumanPawn GetHealee()
{
	local SRKFHumanPawn KFHP, BestKFHP;
	local vector Dir;
	local float TempDot, BestDot;
	local vector Dummy,End,Start;

	Dir = vector(Instigator.GetViewRotation());

	foreach Instigator.VisibleCollidingActors(class'SRKFHumanPawn', KFHP, 80.0)
	{
		if ( KFHP.ShieldStrength < 100 )
		{
			TempDot = Dir dot (KFHP.Location - Instigator.Location);
			if ( TempDot > 0.7 && TempDot > BestDot )
			{
				BestKFHP = KFHP;
				BestDot = TempDot;
			}
		}
	}

	Start = Instigator.Location+Instigator.EyePosition();
	End = Start+vector(Instigator.GetViewRotation())*weaponRange;
	Instigator.bBlockHitPointTraces = false;
	Instigator.Trace(Dummy,Dummy,End,Start,True);
	return BestKFHP;
}

function Actor GetTarget()
{
	local Actor A;
	local vector Dummy,End,Start;

	if( AIController(Instigator.Controller)!=None )
		return Instigator.Controller.Target;
	Start = Instigator.Location+Instigator.EyePosition();
	End = Start+vector(Instigator.GetViewRotation())*weaponRange;
	Instigator.bBlockHitPointTraces = false;
	A = Instigator.Trace(Dummy,Dummy,End,Start,True);
	Instigator.bBlockHitPointTraces = Instigator.default.bBlockHitPointTraces;
	return A;
}

simulated function Timer()
{
	local Actor HitActor;
	local vector StartTrace, EndTrace, HitLocation, HitNormal,AdjustedLocation;
	local rotator PointRot;
	local int MyDamage;
	local int rewardMoney;
	if( !KFWeapon(Weapon).bNoHit )
	{
		MyDamage = MeleeDamage;

		if ( KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo) != none && KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill != none )
		{
			MyDamage = float(MyDamage) * KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo).ClientVeteranSkill.Static.GetWeldSpeedModifier(KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo));
		}
		PointRot = Instigator.GetViewRotation();
		StartTrace = Instigator.Location + Instigator.EyePosition();

		if( AIController(Instigator.Controller)!=None && Instigator.Controller.Target!=None )
		{
			EndTrace = StartTrace + vector(PointRot)*weaponRange;
			Weapon.bBlockHitPointTraces = false;
			HitActor = Trace( HitLocation, HitNormal, EndTrace, StartTrace, true);
			Weapon.bBlockHitPointTraces = Weapon.default.bBlockHitPointTraces;

			if( HitActor==None )
			{
				EndTrace = Instigator.Controller.Target.Location;
				Weapon.bBlockHitPointTraces = false;
				HitActor = Trace( HitLocation, HitNormal, EndTrace, StartTrace, true);
				Weapon.bBlockHitPointTraces = Weapon.default.bBlockHitPointTraces;
			}
			if( HitActor==None )
				HitLocation = Instigator.Controller.Target.Location;
			HitActor = Instigator.Controller.Target;
		}
		else
		{
			EndTrace = StartTrace + vector(PointRot)*weaponRange;
			Weapon.bBlockHitPointTraces = false;
			HitActor = Trace( HitLocation, HitNormal, EndTrace, StartTrace, true);
			Weapon.bBlockHitPointTraces = Weapon.default.bBlockHitPointTraces;
		}

		LastHitActor = KFDoorMover(HitActor);

		if( LastHitActor!=none && Level.NetMode!=NM_Client )
		{
			AdjustedLocation = Hitlocation;
			AdjustedLocation.Z = (Hitlocation.Z - 0.15 * Instigator.collisionheight);

			HitActor.TakeDamage(MyDamage, Instigator, HitLocation , vector(PointRot),hitDamageClass);
			Spawn(class'KFWelderHitEffect',,, AdjustedLocation, rotator(HitLocation - StartTrace));
		}
		else if(CachedHealee !=none && Instigator != none && Level.NetMode!=NM_Client)
		{
			AdjustedLocation = Hitlocation;
			AdjustedLocation.Z = (Hitlocation.Z - 0.15 * Instigator.collisionheight);
			Spawn(class'KFWelderHitEffect',,, AdjustedLocation, rotator(HitLocation - StartTrace));
			rewardMoney = 0;

			if(CachedHealee.ShieldStrength < 10 && KFPlayerController(CachedHealee.Controller).SelectedVeterancy != class'SRVetSupportSpec')
			{
				if(CachedHealee.WeldArmor(float(MyDamage)/50f))
				{
					rewardMoney = 12;
				}
			}
			else
			{
				if(CachedHealee.WeldArmor(float(MyDamage)/12.5f))
				{
					rewardMoney = 3;
				}
			}

			if(SRKFHumanPawn(Instigator) != none)
			{
				KFSteamStatsAndAchievements(KFPlayerController(SRKFHumanPawn(Instigator).Controller).SteamStatsAndAchievements).AddWeldingPoints(MyDamage * 2);
				SRKFHumanPawn(Instigator).PlayerReplicationInfo.Score += rewardMoney;
				KFPlayerReplicationInfo(SRKFHumanPawn(Instigator).PlayerReplicationInfo).ThreeSecondScore += rewardMoney;
				SRKFHumanPawn(Instigator).PlayerReplicationInfo.Team.Score += rewardMoney;
			}
		}
		else if (HitActor == none)
		{
			CachedHealee = none;
		}
	}
}

defaultproperties
{
}
